package vmcs.ui.interfacecontrol;

import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import vmcs.common.Constant;
import vmcs.controller.SimulationController;
import vmcs.simulationsystem.*;

public class JavaSimulatorPanelBuilder extends SimulatorPanelBuilder{
	protected SimulatorControlPanel startUpPanel = null;

	public JavaSimulatorPanelBuilder(
			SimulationController simulationController) {
		super(simulationController);
		this.startUpPanel = new SimulatorControlPanel();
	}
	
	@Override
	public void buildSimulatorPanel() {
	    Font titleFont  = new Font ("Helvetica", Font.BOLD, 18);
		 Label l = new Label (Constant.title);
        l.setBackground(Color.blue);
        l.setForeground(Color.white);
        l.setFont(titleFont);
        
        Frame f = new Frame(Constant.title);
        f.setLayout(new GridLayout(0, 1));
        f.add (l);
        
        this.startUpPanel.setFrame(f);
        this.startUpPanel.setMainCtrl(this.simulationController.getMainController());
	}

	@Override
	public void buildPanelOptions() {
		Button buttons[] = new Button [Constant.NUM_BUTTONS];
		ActionListenerFactory panelButtonListenerFactory = new ActionListenerFactory(startUpPanel.getMainCtrl());
		/*buttons[Constant.SIMUL_BEGIN] = addButton(Constant.SIMUL_BEGIN, Constant.L_SIMUL_BEGIN,
                          new BeginSimulationButtonListener(this.simulationController));
		buttons[Constant.ACT_MAINTAINER] = addButton(Constant.ACT_MAINTAINER, Constant.L_ACT_MAINTAINER,
                          new ActivateMaintainerPanelButtonListener(this.simulationController));
		buttons[Constant.ACT_MACHINERY] = addButton(Constant.ACT_MACHINERY,  Constant.L_ACT_MACHINERY,
                          new ActivateMachineryPanelButtonListener(this.simulationController));
		buttons[Constant.ACT_CUSTOMER] = addButton(Constant.ACT_CUSTOMER,  Constant.L_ACT_CUSTOMER,
                          new ActivateCustomerPanelButtonListener(this.simulationController));
		buttons[Constant.SIMUL_END] = addButton(Constant.SIMUL_END,  Constant.L_SIMUL_END,
                          new EndSimulationButtonListener(this.simulationController.getMainController()));
		*/
		buttons[Constant.SIMUL_BEGIN] = addButton(Constant.SIMUL_BEGIN, Constant.L_SIMUL_BEGIN,panelButtonListenerFactory.getButtonListener(Constant.L_SIMUL_BEGIN));
		buttons[Constant.ACT_MAINTAINER] = addButton(Constant.ACT_MAINTAINER, Constant.L_ACT_MAINTAINER,panelButtonListenerFactory.getButtonListener(Constant.L_ACT_MAINTAINER));
		buttons[Constant.ACT_MACHINERY] = addButton(Constant.ACT_MACHINERY,  Constant.L_ACT_MACHINERY,panelButtonListenerFactory.getButtonListener( Constant.L_ACT_MACHINERY));
		buttons[Constant.ACT_CUSTOMER] = addButton(Constant.ACT_CUSTOMER,  Constant.L_ACT_CUSTOMER,panelButtonListenerFactory.getButtonListener(Constant.L_ACT_CUSTOMER));
		buttons[Constant.SIMUL_END] = addButton(Constant.SIMUL_END,  Constant.L_SIMUL_END,panelButtonListenerFactory.getButtonListener(Constant.L_SIMUL_END));
				
		this.startUpPanel.setButtons(buttons);
        this.startUpPanel.getFrame().pack();
        this.startUpPanel.getFrame().setLocation(100, 100);

        this.startUpPanel.getFrame().addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
            	simulationController.getMainController().closeDown();
                startUpPanel.getFrame().dispose();
            }
        });

	}

	@Override
	public ISimulatorPanel getSimulatorPanel() {
		return this.startUpPanel;
	}
 
    private Button addButton (int id, String label, ActionListener l) {
	    Font buttonFont = new Font ("Helvetica", Font.BOLD, 14);
        Button b = new Button (label);
        b.setBackground(Color.white);
        b.setForeground(Color.blue);
        b.setFont(buttonFont);
        b.addActionListener(l);
        this.startUpPanel.getFrame().add (b);
        return b;
    }

}
